package com.deshan.websys.code.service.impl;

import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deshan.websys.code.mapper.CustomerMapper;
import com.deshan.websys.code.mapper.PmsCommentMapper;
import com.deshan.websys.code.mapper.VisitorMapper;
import com.deshan.websys.code.model.Customers;
import com.deshan.websys.code.model.PmsComment;
import com.deshan.websys.code.model.vo.PmsCommentVO;
import com.deshan.websys.code.service.PmsCommentService;
import com.deshan.websys.code.service.UmsUserService;
import com.deshan.websys.code.service.VisitorsService;
import com.deshan.websys.code.utils.Constant;
import com.deshan.websys.code.utils.SensitiveWordUtils;
import com.deshan.websys.code.utils.StringUtils;
import com.deshan.websys.code.utils.UuidUtils;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class PmsCommentServiceImpl extends ServiceImpl<PmsCommentMapper, PmsComment> implements PmsCommentService {
	@Resource
	PmsCommentMapper pmsCommentMapper;

	@Resource
	private UmsUserService userService;

	@Resource
	private VisitorsService visitorsService;

	@Resource
	private VisitorMapper visitorMapper;

	@Resource
	private CustomerMapper customerMapper;

	@Override
	public int insertPmsComment(PmsComment pmsComment) {
		int result = 0;
		String id = pmsComment.getId();
		pmsComment.setAuditing("0");
		if (StringUtils.isEmpty(id)) {
			pmsComment.setId(UuidUtils.shortUuid());

		}
		result = pmsCommentMapper.insert(pmsComment);
		return result;
	}

	@Override
	public int deletePmsComment(String id) {
		int result = pmsCommentMapper.deleteById(id);

		return result;
	}

	/** 留言审核 */
	@Override
	public int audtingComment(String id) {
		int result = 0;
		PmsComment desPmsComment = pmsCommentMapper.selectById(id);
		if (desPmsComment != null) {
			desPmsComment.setAuditing("1");
			result = pmsCommentMapper.updateById(desPmsComment);
		}
		return result;
	}

	@Override
	public List<PmsComment> findComentlist(String content) {
		QueryWrapper<PmsComment> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isEmpty(content)) {
			queryWrapper.eq("content", content);
		}

		List<PmsComment> resultList = pmsCommentMapper.selectList(queryWrapper);
		return resultList;
	}

	@Override
	public List<PmsComment> findComentbyUser(String memberid) {
		QueryWrapper<PmsComment> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("memberid", memberid);
		List<PmsComment> resultList = pmsCommentMapper.selectList(queryWrapper);
		return resultList;
	}

	@Override
	@Transactional
	public int submitComment(PmsCommentVO pmsCommentVO) {
		Customers customers = customerMapper.selectById(pmsCommentVO.getCustomerId());
		if (Objects.isNull(customers)) {
			log.info("submitComment()---提交留言时，创建用户：{}", pmsCommentVO.getPhone());
			customers = new Customers();
			customers.setId(pmsCommentVO.getCustomerId());
			customers.setEmail(pmsCommentVO.getMail());
			customers.setMainCor(pmsCommentVO.getLegalCode());
			customers.setNickname(Constant.VISIT_USER_NICKNAME_PREFIXX + RandomUtil.randomInt(1, Integer.MAX_VALUE));
			customers.setPhone(pmsCommentVO.getPhone());
			customers.setQq(pmsCommentVO.getQq());
			customers.setWechat(pmsCommentVO.getWx());
			customers.setNameCn(pmsCommentVO.getName());
			customerMapper.insert(customers);
		} else {
			if (!pmsCommentVO.getName().equals(customers.getNameCn()) 
					|| !pmsCommentVO.getPhone().equals(customers.getPhone())) {
				log.info("submitComment()---提交留言时，更新用户姓名为：{},电话为：{}", pmsCommentVO.getName(), pmsCommentVO.getPhone());
				customers.setPhone(pmsCommentVO.getPhone());
				customers.setNameCn(pmsCommentVO.getName());
				customerMapper.updateById(customers);
			}
		}

		PmsComment pmsComment = new PmsComment();
		BeanUtils.copyProperties(pmsCommentVO, pmsComment);
		pmsComment.setContent(SensitiveWordUtils.defaultReplace(pmsComment.getContent()));
		pmsComment.setCid(customers.getId());
		return pmsCommentMapper.insert(pmsComment);
	}
}
